package com.weiqk.pmdemo.controller;

import com.alibaba.excel.EasyExcel;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.weiqk.pmdemo.controller.R.R;
import com.weiqk.pmdemo.dto.ExcelDto;
import com.weiqk.pmdemo.excel.ExcelListener;
import com.weiqk.pmdemo.excel.outExcel;
import com.weiqk.pmdemo.pojo.Project;
import com.weiqk.pmdemo.service.ProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
@Api(tags="导出导入接口")
@Slf4j
@RestController
@RequestMapping("/Excel")
public class ExcelController {
    @Autowired
    ProjectService projectService;
    @Autowired
    outExcel outExcel;
    @ApiOperation("传入excel")
    @ApiOperationSupport(author = "EDSION")
    @PostMapping("/putExcel")
    public R putExcel(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), Project.class, new ExcelListener<Project>(projectService)).sheet().doRead();
        return new R(true,null);
    }
    @ApiOperation("输出excel到指定路径")
    @ApiOperationSupport(author = "EDSION")
    @ResponseBody
    @PostMapping("/outExcel")
    public R addExcel(@RequestBody ExcelDto excelDto) throws IOException {
       return new R(true,outExcel.writeDbtoExcel(excelDto.getPath(),excelDto.getFileName()));
    }
}
